<?php

/**
 * This is the model class for table "es_region".
 *
 * The followings are the available columns in table 'es_region':
 * @property integer $region_id
 * @property integer $parent_id
 * @property string $region_name
 * @property integer $region_type
 * @property integer $agency_id
 */
class Region extends CActiveRecord
{

	public $province_id;
	public $city_id;
	public $area_id;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Region the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'es_region';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_id, region_type, agency_id', 'numerical', 'integerOnly'=>true),
			array('region_name', 'length', 'max'=>120),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('region_id, parent_id, region_name, region_type, agency_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'region_id' => 'Region',
			'parent_id' => 'Parent',
			'region_name' => 'Region Name',
			'region_type' => 'Region Type',
			'agency_id' => 'Agency',
			'province'=>'Province',
			'city'=>'City',
			'area'=>'Area',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('region_id',$this->region_id);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('region_name',$this->region_name,true);
		$criteria->compare('region_type',$this->region_type);
		$criteria->compare('agency_id',$this->agency_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * @return array province names indexed by region IDs
	 */
	public function getProvinceOpinions()
	{
		$sql = 'select * from es_region where parent_id = 1';
		$model = $this->findAllBySql($sql);
		$provinceArray = CHtml::listData($model, 'region_id', 'region_name');
		return $provinceArray;
	}
	
	/**
	 * @return array city names indexed by region IDs
	 */
	public function getCityOpinions($id)
	{
		$sql = "select * from es_region where parent_id = $id";
		$model = $this->findAllBySql($sql);
		$cityArray = CHtml::listData($model, 'region_id', 'region_name');
		return $cityArray;
	}
	
	/**
	 * @return array area names indexed by region IDs
	 */
	public function getAreaOpinions($id)
	{
		$sql = "select * from es_region where parent_id = $id";
		$model = $this->findAllBySql($sql);
		$areaArray = CHtml::listData($model, 'region_id', 'region_name');
		return $areaArray;
	}
}